package com.codebattery.service;

import com.codebattery.client.GoodsServiceClient;
import com.codebattery.client.OrderServiceClient;
import com.codebattery.domain.model.Cart;
import com.codebattery.domain.param.CartItemInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;


@Service
@Slf4j
public class CartService {
    @Resource
    private OrderServiceClient orderServiceClient;
    @Resource
    private GoodsServiceClient goodsServiceClient;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 获取用户的购物车
     *
     * @param userId
     * @return
     */
    public Cart getCart(Integer userId){
        HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();

        Cart cart =  (Cart) Optional.ofNullable(hashOperations.get("cart:" + userId, "cart"))
                .orElse(new Cart());

        return cart;
    }

    public Cart addItem(CartItemInfo cartItemInfo){

        HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();

        Cart cart =  (Cart) Optional.ofNullable(hashOperations.get("cart:" + cartItemInfo.getUserId(), "cart"))
                                    .orElse(new Cart());

        cart.setAmount(cart.getAmount() + cartItemInfo.getCount()); //商品总件数
        cart.setPrice(cart.getPrice().add(cartItemInfo.getPrice()));    //商品总价格
        cart.setCreateTime(new Date());

        Cart.CartItem cartItem = new Cart.CartItem(
                cartItemInfo.getGoodsId(),
                cartItemInfo.getTitle(),
                cartItemInfo.getCount(),
                cartItemInfo.getPrice(),
                cartItemInfo.getImg(),
                cartItemInfo.getMeal(),
                new Date(),
                new Date()
        );

        List<Cart.CartItem> items = Optional.ofNullable(cart.getItems()).orElse(new ArrayList<>());
        items.add(cartItem);
        cart.setItems(items);

        hashOperations.put("cart:" + cartItemInfo.getUserId(), "cart", cart);

        return cart;
    }

    /**
     * 删除项目
     *
     */
    public void deleteItem(String userId, Integer goodsId){

        HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();

        Optional.ofNullable((Cart)hashOperations.get("cart:" + userId, "cart"))
                .map(cart -> cart.getItems())
                .ifPresent(items -> {
                    items.removeIf(cartItem -> cartItem.getGoodsId().equals(goodsId));
                });
    }



    /**
     * 清空购物车
     *
     * @param userId  用户ID
     */
    public void empty(String userId) {
        HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
        Optional.ofNullable(hashOperations.get("cart:" + userId, "cart"))
                .ifPresent((cart) -> {
                    hashOperations.delete("cart:" + userId, "cart");
                    log.info("清空用户[{}]购物车成功", userId);
                });
    }
}
